/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managerBeanAdmin;

import entity.Category;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import processor.CategoryFacade;

/**
 *
 * @author Phong
 */
@ManagedBean
@SessionScoped
public class CategoryMBean {

    @EJB
    private CategoryFacade categoryFacade;
    private List<Category> categories;
    private List<Category> filteredCategories;
    private Category selectedCategory = new Category();
    private Integer categoryID;
    private String categoryName;
    private String categoryPriority;

    /**
     * Creates a new instance of CategoryMBean
     */
    public CategoryMBean() {
    }

    /**
     * @return the categories
     */
    public List<Category> getCategories() {
        categories = categoryFacade.getCategoryAll();
        return categories;
    }

    /**
     * @param categories the categories to set
     */
    public void setCategories(List<Category> categories) {
        this.setCategories(categories);
    }

    //Add category function: 
    public void createCategory() {
        FacesMessage msg;
        try {
            if (categoryFacade.createCategory(categoryName, categoryPriority)) {
                msg = new FacesMessage("Created department: ", categoryName + " successfully.");
            } else {
                msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Created department: ", categoryName + " failed.");
            }
        } catch (Exception e) {
            msg = new FacesMessage("Error: ", e.getMessage());
        }
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    //Edit category function
    public void editCategory(ActionEvent actionEvent) {
        FacesMessage msg;
        try {
            if (categoryFacade.getCategory(selectedCategory.getCategoryID()) != null) {
                if (categoryFacade.editCategory(selectedCategory.getCategoryID(),
                        selectedCategory.getCategoryName(),
                        selectedCategory.getCategoryPriority())) {
                    msg = new FacesMessage("Edited department: ", selectedCategory.getCategoryName() + " successfully.");
                } else {
                    msg = new FacesMessage("Edited department: ", selectedCategory.getCategoryName() + " failed.");
                }
            } else {
                msg = new FacesMessage("Department: ", selectedCategory.getCategoryName() + " is existed.");
            }
        } catch (Exception e) {
            msg = new FacesMessage("Error: ", e.getMessage());
        }
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public String actionEditCategory(Category inputC) {
        selectedCategory = inputC;
        return "editCategory.xhtml";
    }

    //Remove category function: 
    public void removeCategory(ActionEvent actionEvent) {
        FacesMessage msg;
        try {
            if (categoryFacade.removeCategory(selectedCategory.getCategoryID())) {
                msg = new FacesMessage("Category removed: ", selectedCategory.getCategoryName() + " successfully.");
            }else{
                 msg = new FacesMessage("Category removed: ", selectedCategory.getCategoryName() + " failed.");
            }
        } catch (Exception e) {
            msg = new FacesMessage("Error: ", e.getMessage());
        }
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public String getErrorMessage(final String clientId) {
        if (clientId.equals("form:categoryName")) {
            return "Name is required and betwwen 3 to 200 letters";
        } else {
            Iterator<FacesMessage> iter = FacesContext.getCurrentInstance().getMessages(clientId);
            if (iter.hasNext()) {
                return iter.next().getDetail(); // or getSummary()
            }
            return "";
        }
    }

    /**
     * @return the filteredCategories
     */
    public List<Category> getFilteredCategories() {
        return filteredCategories;
    }

    /**
     * @param filteredCategories the filteredCategories to set
     */
    public void setFilteredCategories(List<Category> filteredCategories) {
        this.filteredCategories = filteredCategories;
    }

    /**
     * @return the selectedCategory
     */
    public Category getSelectedCategory() {
        return selectedCategory;
    }

    /**
     * @param selectedCategory the selectedCategory to set
     */
    public void setSelectedCategory(Category selectedCategory) {
        this.selectedCategory = selectedCategory;
    }

    /**
     * @return the categoryName
     */
    public String getCategoryName() {
        return categoryName;
    }

    /**
     * @param categoryName the categoryName to set
     */
    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    /**
     * @return the categoryPriority
     */
    public String getCategoryPriority() {
        return categoryPriority;
    }

    /**
     * @param categoryPriority the categoryPriority to set
     */
    public void setCategoryPriority(String categoryPriority) {
        this.categoryPriority = categoryPriority;
    }

    /**
     * @return the categoryID
     */
    public Integer getCategoryID() {
        return categoryID;
    }

    /**
     * @param categoryID the categoryID to set
     */
    public void setCategoryID(Integer categoryID) {
        this.categoryID = categoryID;
    }
}
